﻿package foli
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import sk.prasa.webapis.picasa.PicasaService;
	import sk.prasa.webapis.picasa.events.PicasaEvent;
	import sk.prasa.webapis.picasa.Photo;
	import sk.prasa.webapis.picasa.GPhoto;

	public class Album extends EventDispatcher
	{
		public var id: String;
		public var user: String;

		/// construct from username and album id 
		public function Album( uid:String, aid:String )
		{
			id = aid;
			user = uid;					
		}
		
		/// asyncronously load information about photos contained in that album
		public function loadFotoList():void
		{
			var service:PicasaService = new PicasaService  ;
			service.addEventListener( PicasaEvent.PHOTOS_GET_LIST, this.handleFotoList );
			service.photos.list( user , id );			
		}
		
		/// load callback for loading of photo_list
		private function handleFotoList(evt:PicasaEvent)
		{
			if (evt.success)
			{				
				var numFotos: int = evt.data.length;

				// for each photo in the result array
				for (var i:Number=0; i < numFotos; i++)
				{
					var p: Photo = evt.data[i];
					var g: GPhoto = p.gphoto;
	
					// create new Foto and inform listeners
					var f: Foto = new Foto(g.id);
					dispatchEvent( new AlbumEvent(AlbumEvent.FOTO_ADDED, f) );
				}
			}
			else
			{
				trace(evt.error.message);
			}
			evt.target.removeEventListener(evt.type,this.handleFotoList);
		}		
	}
}